package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IPermissionBizService;
import com.kk.xhr.admin.controller.request.PermissionAddRequest;
import com.kk.xhr.admin.controller.request.PermissionModifyRequest;
import com.kk.xhr.admin.controller.vo.PermissionTreeVO;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.web.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 权限控制器
 *
 * @author kkmystery
 * @version 1.0 2021/3/11
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "权限管理", tags = "权限管理")
@RequestMapping("/permission")
public class PermissionController {
    private final Executor adminControllerBizExecutor;
    private final IPermissionBizService permissionBizService;

    @ApiOperation(value = "获取权限信息")
    @RequiresRoles(value = {"admin"})
    @GetMapping("/{id:.+}")
    public CompletionStage<ApiResult<PermissionTreeVO>> query(@PathVariable("id") Long id) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(permissionBizService.query(id)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "根据角色获取权限id列表")
    @RequiresRoles(value = {"admin"})
    @GetMapping("/role/{id:.+}")
    public CompletionStage<ApiResult<List<Long>>> queryRolePermission(@PathVariable("id") Long roleId) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(permissionBizService.queryPermissionByRole(roleId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加权限信息")
    @RequiresRoles(value = {"admin"})
    @PostMapping
    public CompletionStage<ApiResult<Long>> add(@RequestBody PermissionAddRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            Long roleId = permissionBizService.add(request);
            return ApiResult.success(roleId);
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "给角色添加权限信息")
    @RequiresRoles(value = {"admin"})
    @PostMapping("/role/{id:.+}")
    public CompletionStage<ApiResult<Void>> upsert(@PathVariable("id") Long roleId, @RequestBody List<Long> ids) {
        return CompletableFuture.supplyAsync(() -> {
            permissionBizService.upsertToRole(roleId, ids);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }


    @ApiOperation(value = "修改权限信息")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/{id:.+}")
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, @RequestBody PermissionModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            permissionBizService.modify(id, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改权限状态")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/status/{id:.+}")
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, BinaryEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            permissionBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}


